package java04;

class ArrayIntegerStack implements IntegerStack {
	private Integer[] stack;
	private int num=0;
	public ArrayIntegerStack(int n) {
		this.setStack(new Integer[n]);
	}

	@Override
	public Integer push(Integer item) {
		if(this.size() >= this.stack.length )return null;
		if (item == null)
			return null;
		this.stack[this.num] = item;
		this.num++;
		return item;          

	}
	@Override
	public Integer pop() {
		if(this.empty()) return null;
		if (this.stack[num-1] == null)
			return null;
		this.num--;
		return this.stack[num];
	}

	@Override
	public Integer peek() {
		if (num == 0)
			return null;
		int number = 0;
		for (int i = 0; i < this.stack.length; i++) {
			if (this.stack[i] != null)
				number++;
		}
		if (number != this.num)
			return this.stack[this.num - 1];
		for (int i = 0; i < this.stack.length; i++) {
			if (this.stack[i] == null)
				return this.stack[this.num - 1];
		}
		return this.  stack[this.num - 1];
	}

	@Override
	public boolean empty() {
		if (this.num == 0)
			return true;
		return false;
	}

	@Override
	public int size() {
		return this.num;
	}

	public Integer[] getStack() {
		return stack;
	}

	public void setStack(Integer[] stack) {
		this.stack = stack;
	}

}
